home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / p_man / cat3 / librw / RWTPtrHashDictionary.z / RWTPtrHashDictionary
Encoding:
Text File  |  2002-10-03  |  14.2 KB  |  331 lines

  1.  
  2.  
  3.  
  4. RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))                          RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))
  5.  
  6.  
  7.  
  8. NNNNaaaammmmeeee
  9.      RWTPtrHashDictionary<K,V> - Rogue Wave library class
  10.  
  11. SSSSyyyynnnnooooppppssssiiiissss
  12.               #include <rw/tphdict.h>
  13.  
  14.  
  15.  
  16.               unsigned hashFun(const K&);
  17.           RWTPtrHashDictionary<K,V> dictionary(hashFun);
  18.  
  19. PPPPlllleeeeaaaasssseeee NNNNooootttteeee!!!!
  20.      IIIIffff yyyyoooouuuu ddddoooo nnnnooootttt hhhhaaaavvvveeee tttthhhheeee SSSSttttaaaannnnddddaaaarrrrdddd CCCC++++++++ LLLLiiiibbbbrrrraaaarrrryyyy,,,, uuuusssseeee tttthhhheeee iiiinnnntttteeeerrrrffffaaaacccceeee ddddeeeessssccccrrrriiiibbbbeeeedddd
  21.      hhhheeeerrrreeee....  OOOOtttthhhheeeerrrrwwwwiiiisssseeee,,,, uuuusssseeee tttthhhheeee iiiinnnntttteeeerrrrffffaaaacccceeee ttttoooo RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhMMMMaaaapppp described in the
  22.      Class Reference.
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. DDDDeeeessssccccrrrriiiippppttttiiiioooonnnn
  30.       RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy<<<<KKKK,,,,VVVV>>>> is a dictionary of keys of type KKKK and values
  31.      of type VVVV, implemented using a hash table.  While duplicates of values
  32.      are allowed, duplicates of keys are not.  It is a ppppooooiiiinnnntttteeeerrrr based
  33.      collection: pointers to the keys and values are copied in and out of the
  34.      hash buckets. Parameters KKKK and VVVV represent the type of the key and the
  35.      type of the value, respectively, to be inserted into the table.  These
  36.      can be either classes or fundamental types.  Class KKKK must have
  37.           well-defined equality semantics (KKKK::::::::ooooppppeeeerrrraaaattttoooorrrr========((((ccccoooonnnnsssstttt KKKK&&&&))))).
  38.  
  39.      Class VVVV can be of any type.A user-supplied hashing function for type KKKK
  40.      must be supplied to the constructor when creating a new table.  If KKKK is a
  41.      Rogue Wave class, then this requirement is usually trivial because most
  42.      Rogue Wave objects know how to return a hashing value.  In fact, classes
  43.      RRRRWWWWCCCCSSSSttttrrrriiiinnnngggg, RRRRWWWWDDDDaaaatttteeee, RRRRWWWWTTTTiiiimmmmeeee, and RRRRWWWWWWWWSSSSttttrrrriiiinnnngggg contain static member functions
  44.      called hhhhaaaasssshhhh that can be supplied to the constructor as is.  The function
  45.      must have prototype:
  46.  
  47.               unsigned hhhhFFFFuuuunnnn(const K& a);
  48.  
  49.  
  50.  
  51.  
  52.  
  53.      and should return a suitable hash value for the object aaaa. To find a
  54.      value, the key is first hashed to determine in which bucket the key and
  55.      value can be found.  The bucket is then searched for an object that is
  56.      equal (as determined by the equality operator) to the key.  The initial
  57.      number of buckets in the table is set by the constructor.  There is a
  58.      default value.  If the number of (key/value) pairs in the collection
  59.      greatly exceeds the number of buckets then efficiency will sag because
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))                          RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))
  71.  
  72.  
  73.  
  74.      each bucket must be searched linearly.  The number of buckets can be
  75.      changed by calling member function rrrreeeessssiiiizzzzeeee(((()))).  This is relatively
  76.      expensive because all of the keys must be rehashed. If you wish for this
  77.      to be done automatically, then you can subclass from this class and
  78.      implement your own special iiiinnnnsssseeeerrrrtttt(((()))) and rrrreeeemmmmoooovvvveeee(((()))) functions which perform
  79.      a rrrreeeessssiiiizzzzeeee(((()))) as necessary.
  80.  
  81. PPPPeeeerrrrssssiiiisssstttteeeennnncccceeee
  82.      None
  83.  
  84. EEEExxxxaaaammmmpppplllleeee
  85.               #include <rw/tphdict.h>
  86.           #include <rw/cstring.h>
  87.           #include <rw/rwdate.h>
  88.           #include <rw/rstream.h>
  89.           main()  {
  90.             RWTPtrHashDictionary<RWCString, RWDate>
  91.               birthdays(RWCString::hash);
  92.             birthdays.insertKeyAndValue
  93.               (new RWCString("John"),
  94.                new RWDate(12, "April", 1975)
  95.               );
  96.             birthdays.insertKeyAndValue
  97.               (new RWCString("Ivan"),
  98.                new RWDate(2, "Nov", 1980)
  99.               );
  100.             // Alternative syntax:
  101.             birthdays[new RWCString("Susan")] =
  102.               new RWDate(30, "June", 1955);
  103.             birthdays[new RWCString("Gene")] =
  104.               new RWDate(5, "Jan", 1981);
  105.             // Print a birthday:
  106.             RWCString key("John");
  107.             cout << *birthdays[&key] << endl;
  108.             birthdays.clearAndDestroy();
  109.             return 0;
  110.           }
  111.  
  112.  
  113.      Program output:
  114.  
  115.               April 12, 1975
  116.  
  117.  
  118.  
  119.  
  120. PPPPuuuubbbblllliiiicccc CCCCoooonnnnssssttttrrrruuuuccccttttoooorrrrssss
  121.               RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy<K,V>(unsigned (*hashKey)(const K&),
  122.                                  size_t buckets = RWDEFAULT_CAPACITY);
  123.  
  124.  
  125.      Constructs an empty hash dictionary.  The first argument is a pointer to
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))                          RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))
  137.  
  138.  
  139.  
  140.      a user-defined hashing function for items of type KKKK (the key).  The table
  141.      will initally have bbbbuuuucccckkkkeeeettttssss buckets although this can be changed with
  142.      member function rrrreeeessssiiiizzzzeeee(((()))).
  143.  
  144.               RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy<K,V>(const RWTPtrHashDictionary<K,V>& c);
  145.  
  146.  
  147.      Constructs a new hash dictionary as a shallow copy of cccc.  After
  148.      construction, pointers will be shared between the two collections.  The
  149.      new object will use the same hashing function and have the same number of
  150.      buckets as cccc.  Hence, the keys will not be rehashed.
  151.  
  152. PPPPuuuubbbblllliiiicccc OOOOppppeeeerrrraaaattttoooorrrrssss
  153.               RWTPtrHashDictionary<K,V>&
  154.           ooooppppeeeerrrraaaattttoooorrrr====(const RWTPtrHashDictionary<K,V>& c);
  155.  
  156.  
  157.      Sets self to a shallow copy of cccc.  Afterwards, pointers will be shared
  158.      between the two collections.  Self will use the same hashing function and
  159.      have the number of buckets as cccc.  Hence, the keys will not be rehashed.
  160.  
  161.               V*&
  162.           ooooppppeeeerrrraaaattttoooorrrr[[[[]]]](K* key);
  163.  
  164.  
  165.      Look up the key kkkkeeeeyyyy and return a reference to the pointer of its
  166.      associated value.  If the key is not in the dictionary, then it is added
  167.      to the dictionary.  In this case, the pointer to the value will be
  168.      undefined.  Because of this, if there is a possibility that a key will
  169.      not be in the dictionary, then this operator can only be used as an
  170.      llllvvvvaaaalllluuuueeee.
  171.  
  172. PPPPuuuubbbblllliiiicccc MMMMeeeemmmmbbbbeeeerrrr FFFFuuuunnnnccccttttiiiioooonnnnssss
  173.               void
  174.           aaaappppppppllllyyyyTTTTooooKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee( void (*applyFun)(K*,V*&,void*),void* d);
  175.  
  176.  
  177.      Applies the user-defined function pointed to by aaaappppppppllllyyyyFFFFuuuunnnn to every key-
  178.      value pair in the dictionary.  This function must have prototype:
  179.  
  180.               void yyyyoooouuuurrrrFFFFuuuunnnn(K* key, V*& value, void* d);
  181.  
  182.  
  183.  
  184.  
  185.  
  186.      This function will be called for each key value pair in the dictionary,
  187.      with a pointer to the key as the first argument and a reference to a
  188.      pointer to the value as the second argument.  The key should not be
  189.      changed or touched.  A new value can be substituted, or the old value can
  190.      be changed.  Client data may be passed through as parameter dddd.
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))                          RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))
  203.  
  204.  
  205.  
  206.               void
  207.           cccclllleeeeaaaarrrr();
  208.  
  209.  
  210.      Removes all key value pairs from the collection.
  211.  
  212.               void
  213.           cccclllleeeeaaaarrrrAAAAnnnnddddDDDDeeeessssttttrrrrooooyyyy();
  214.  
  215.  
  216.      Removes all key value pairs from the collection aaaannnndddd deletes both the keys
  217.      and the values.
  218.  
  219.               RWBoolean
  220.           ccccoooonnnnttttaaaaiiiinnnnssss(const K* key) const;
  221.  
  222.  
  223.      Returns TTTTRRRRUUUUEEEE if the dictionary contains a key which is equal to the key
  224.      pointed to by kkkkeeeeyyyy.  Returns FFFFAAAALLLLSSSSEEEE otherwise.  Equality is measured by the
  225.      class-defined equality operator for type KKKK.
  226.  
  227.               size_t
  228.           eeeennnnttttrrrriiiieeeessss() const;
  229.  
  230.  
  231.      Returns the number of key-value pairs currently in the dictionary.
  232.  
  233.               K*
  234.           ffffiiiinnnndddd(const K* key) const;
  235.  
  236.  
  237.      Returns a pointer to the kkkkeeeeyyyy which is equal to the key pointed to by kkkkeeeeyyyy,
  238.      or nnnniiiillll if no such item could be found.  Equality is measured by the
  239.      class-defined equality operator for type KKKK.
  240.  
  241.               V*
  242.           ffffiiiinnnnddddVVVVaaaalllluuuueeee(const K* key) const;
  243.  
  244.  
  245.      Returns a pointer to the vvvvaaaalllluuuueeee associated with the key pointed to by kkkkeeeeyyyy,
  246.      or nnnniiiillll if no such item could be found.  Equality is measured by the
  247.      class-defined equality operator for type KKKK.
  248.  
  249.               K*
  250.           ffffiiiinnnnddddKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee(const K* key, V*& retVal) const;
  251.  
  252.  
  253.      Returns a pointer to the kkkkeeeeyyyy associated with the key pointed to by kkkkeeeeyyyy,
  254.      or nnnniiiillll if no such item could be found.  If a key is found, the pointer to
  255.      its associated value is put in rrrreeeettttVVVVaaaallll.  Equality is measured by the
  256.      class-defined equality operator for type KKKK.
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))                          RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))
  269.  
  270.  
  271.  
  272.               void
  273.           iiiinnnnsssseeeerrrrttttKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee(K* key, V* value);
  274.  
  275.  
  276.      If the key pointed to by kkkkeeeeyyyy is in the dictionary, then its associated
  277.      value is changed to vvvvaaaalllluuuueeee.  Otherwise, a new key value pair is inserted
  278.      into the dictionary.
  279.  
  280.               RWBoolean
  281.           iiiissssEEEEmmmmppppttttyyyy() const;
  282.  
  283.  
  284.      Returns TTTTRRRRUUUUEEEE if the dictionary has no items in it, FFFFAAAALLLLSSSSEEEE otherwise.
  285.  
  286.               K*
  287.           rrrreeeemmmmoooovvvveeee(const K* key);
  288.  
  289.  
  290.      Removes the key and value pair where the key is equal to the key pointed
  291.      to by kkkkeeeeyyyy.  Returns the kkkkeeeeyyyy or nnnniiiillll if no match was found.  Equality is
  292.      measured by the class-defined equality operator for type KKKK.
  293.  
  294.               void
  295.           rrrreeeessssiiiizzzzeeee(size_t N);
  296.  
  297.  
  298.      Changes the number of buckets to NNNN.  This will result in all of the keys
  299.      being rehashed.
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.